/*
 * Copyright 2020 Liu Jie <jieliu2000@live.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beyondsoft.sabg.edu.model.db.core;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Liu Jie <jieliu2000@live.com>
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "nothing-binaryFile")
public class BinaryFile extends BasicEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
    @Size(max = 100)
    private String originalName;
    private String storeLocation;

    @Size(max = 100)
    private String fileType;
    @JsonIgnore
    @Size(max = 500)
    String contentType;
    @JsonIgnore
    @Lob
    byte[] binaryContents;

    @Size(max = 500)
    String name;

    @Size(max = 200)
    String extInfo;


	public String getUrlId() {
		if (this.getId() == null) {
			return null;
		}
		String idString = this.getId() + "";

		// String urlId = Base64Utils.
		String result = Base64Utils.encodeToUrlSafeString(idString.getBytes());
		return result;
	}

    public String getStoreLocation() {
        return storeLocation;
    }

    public void setStoreLocation(String storeLocation) {
        this.storeLocation = storeLocation;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public byte[] getBinaryContents() {
        return binaryContents;
    }

    public void setBinaryContents(byte[] binaryContents) {
        this.binaryContents = binaryContents;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getExtInfo() {
        return extInfo;
    }

    public void setExtInfo(String extInfo) {
        this.extInfo = extInfo;
    }
    public String getOriginalName() {
        return originalName;
    }

    public void setOriginalName(String originalName) {
        this.originalName = originalName;
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }
    public void converFromMultipartFile(MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        this.setStoreLocation("db");
        this.setName(file.getOriginalFilename());
        this.setContentType(file.getContentType());
        this.setBinaryContents(file.getBytes());

    }
}
